package com.hsmw.api.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hsmw.api.mapper.HsmwConstructionUnitMapper;
import com.hsmw.api.mapper.HsmwLeasingCompanyMapper;
import com.hsmw.common.base.BaseService;
import com.hsmw.domain.HsmwConstructionUnit;
import com.hsmw.domain.HsmwLeasingCompany;
import com.hsmw.domain.HsmwUser;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author WD
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwConstructionUnitService extends BaseService<HsmwConstructionUnitMapper, HsmwConstructionUnit> {

	@Autowired
	private HsmwConstructionUnitMapper hsmwConstructionUnitMapper;


	public IPage<HsmwConstructionUnit> getHsmwConstructionUnitPage(IPage page, HsmwConstructionUnit hsmwConstructionUnit) {
		LambdaQueryWrapper<HsmwConstructionUnit> queryWrapper = new LambdaQueryWrapper<>();
		if (!StrUtil.isBlank(hsmwConstructionUnit.getName())) {
			queryWrapper.like(HsmwConstructionUnit::getName, hsmwConstructionUnit.getName());
		}
		if (!StrUtil.isBlank(hsmwConstructionUnit.getTel())) {
			queryWrapper.eq(HsmwConstructionUnit::getTel, hsmwConstructionUnit.getTel());
		}
		if (!StrUtil.isBlank(hsmwConstructionUnit.getLinkMan())) {
			queryWrapper.eq(HsmwConstructionUnit::getLinkMan, hsmwConstructionUnit.getLinkMan());
		}
		IPage ipage = this.hsmwConstructionUnitMapper.selectPage(page, queryWrapper.orderByDesc(HsmwConstructionUnit::getCreateTime));
		return ipage;
	}

	public HsmwConstructionUnit getHsmwConstructionUnitById(String key) {
		return this.getById(key);
	}

	public void saveOrUpdateHsmwConstructionUnit(HsmwConstructionUnit hsmwConstructionUnit) {
		if (StrUtil.isBlank(hsmwConstructionUnit.getId())) {
			hsmwConstructionUnitMapper.insert(hsmwConstructionUnit);
		} else {
			hsmwConstructionUnitMapper.updateById(hsmwConstructionUnit);
		}
	}

	/**
	 * 新增或者修改公司信息
	 * @param hsmwConstructionUnit
	 * @return
	 */
	public DataResponse addCompany(HsmwConstructionUnit hsmwConstructionUnit) {
		// 修改
		if(StringUtils.isNotBlank(hsmwConstructionUnit.getId())){

			return new DataResponse(hsmwConstructionUnitMapper.updateById(hsmwConstructionUnit));

		}else {

			return new DataResponse(hsmwConstructionUnitMapper.insert(hsmwConstructionUnit));

		}
	}


}
